﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using cocos2d;

namespace CampaignWP7
{
    class ProgressBar:CCSprite
    {
        //
        public int MaxProgress;
        public int CurrentProgress;
        private const int MaxSprites = 10;
        private const string cellSpriteFile = @"images\Turnip";//萝卜图片  
        private List<CCSprite> mSprites;

        public int width;
        public int height;
        private int cellWidth;
        private int cellHeight;



        public ProgressBar(int maxProgress, int currentProgress)
        {
            MaxProgress = maxProgress;
            CurrentProgress = currentProgress;
            mSprites = new List<CCSprite>(MaxSprites);
            initWithFile(ResDef.g_BlankTextureFile);
            this.anchorPoint = new CCPoint(0, 0.5f);    //左边中心是锚点               
            for (int i = 0; i < MaxSprites;++i )
            {
                CCSprite cell = CCSprite.spriteWithFile(cellSpriteFile);
                cell.anchorPoint = new CCPoint(0, 0.5f);
                //
                addChild(cell);
                mSprites.Add(cell);
                cell.position = new CCPoint(width, 0);
                cell.visible = false;
                //
                cellWidth = cell.Texture.PixelsWide;
                cellHeight = cell.Texture.PixelsHigh;
                //

                width = width + cellWidth;
                height = cellHeight;
                //
                             
            }

        }
        public void SetCurrentProgress(int g)
        {
            CurrentProgress = g;
        }
        //
        public override void update(float dt)
        {
            int pole = (int)((float)CurrentProgress / (float)MaxProgress * MaxSprites);
            for (int i = 0; i < MaxSprites; ++i)
            {
                if (i < pole)
                {
                    mSprites[i].visible = true;
                }
                else
                {
                    mSprites[i].visible = false;
                }
                //
//                 mSprites[i].position = new CCPoint(this.position.x + cellWidth * i, 
//                                                    this.position.y);
            }
            base.update(dt);
        }
    }
}
